import {MenuOption, CascaderOption} from "naive-ui"
import {arrCopyDeep} from "st-common-core"
import {MenuType} from "../types/menu-type.ts"
import {MenuObjPropNameEnum} from "../enums/menu-obj-prop-name-enum.ts"
import {renderMenuIconHandler} from '../utils/menu-render-util.ts'

/**
 * 生成菜单选项
 *
 * @param {Array<MenuType>} menus 菜单信息数据
 * @returns {MenuOption[]} 菜单选项
 */
export const genMenuOptions = (menus: Array<MenuType>): MenuOption[] => {
  return arrCopyDeep({
    origin: menus,
    props: [
      MenuObjPropNameEnum.ID,
      MenuObjPropNameEnum.TYPE,
      MenuObjPropNameEnum.NAME,
      MenuObjPropNameEnum.PAGE_PATH,
      MenuObjPropNameEnum.ROUTE_NAME,
      MenuObjPropNameEnum.ROUTE_PATH,
      MenuObjPropNameEnum.ROUTE_QUERY,
      MenuObjPropNameEnum.LINK_PATH,
      MenuObjPropNameEnum.SORT,
      MenuObjPropNameEnum.ICON_TYPE,
      MenuObjPropNameEnum.ICON,
      MenuObjPropNameEnum.DISABLED,
      MenuObjPropNameEnum.HIDDEN,
      MenuObjPropNameEnum.CHILDREN,
      MenuObjPropNameEnum.PATH,
    ],
    propsMap: {
      [MenuObjPropNameEnum.ID]: ['value', 'key'],
      [MenuObjPropNameEnum.NAME]: 'label',
    },
    targetPropValMap: {
      [MenuObjPropNameEnum.ICON]: (origin) => (() => renderMenuIconHandler(origin)),
      [MenuObjPropNameEnum.DISABLED]: (origin) => Boolean(origin[MenuObjPropNameEnum.DISABLED]),
      [MenuObjPropNameEnum.HIDDEN]: (origin) => Boolean(origin[MenuObjPropNameEnum.HIDDEN])
    },
    excludeArrItem: (item) => item[MenuObjPropNameEnum.HIDDEN],
    sort: (a, b) => b[MenuObjPropNameEnum.SORT] - a[MenuObjPropNameEnum.SORT],
    isCopyEmptyArr: false,
  }) as MenuOption[]
}

/**
 * 生成菜单级联选项
 *
 * @param {Array<MenuType>} menus 菜单信息数据
 * @returns {Array<CascaderOption>} 菜单级联选项
 */
export const genMenuCascadeOptions = (menus: Array<MenuType>): Array<CascaderOption> => {
  return arrCopyDeep({
    origin: menus,
    props: [
      MenuObjPropNameEnum.ID,
      MenuObjPropNameEnum.NAME,
      MenuObjPropNameEnum.CHILDREN,
    ],
    propsMap: {
      [MenuObjPropNameEnum.ID]: ['value', 'key'],
      [MenuObjPropNameEnum.NAME]: 'label',
    },
    isCopyEmptyArr: false,
  }) as Array<CascaderOption>
}
